package boundarys;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;

import javax.swing.JFrame;

import common.ConIF;
import client.CCRMClient;
import runFromHere.ClientGUI;
import ccrm.BoundryControl;
import entitys.Session;
import enums.Result;

/**
 * General GUI for all GUIs in project.
 * All GUIs that extends this class must implement getReply() method and may override other methods if needed. 
 * made for easy navigation between GUIs and for easy handling with messages from server. 
 * Defining general-most common properties for GUIs in the project.
 */
public abstract class AbstractGUI {
	/**
	 * Child's frame.
	 * initiate by calling super() constructor from child's constructor.  
	 */
	protected JFrame frame;
	/**
	 * GUI's index in the GUIs' array. 
	 */
	protected int index;
	
	
	
	/**
	 *  Abstract method for easy handling with messages from server.
	 *  ALL GUIs must implement this method in order to get messages from server. 
	 */
	public abstract void getReply(Object r);
	

	/**
	 *  Defining general-most common properties for GUIs in the project.
	 */
	protected void initGUI(JFrame f)
	{
		frame=f;
		index = BoundryControl.GUIArr.size();
		BoundryControl.GUIArr.add(this);
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frame.setResizable(false);
		frame.setVisible(true);
		frame.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				
				goBack();
			}
		});
	}
	/**
	 * Initiate child's frame.
	 */
	public void setFrame(JFrame f)
	{
		frame = f;
	}
	/**
	 * Returning child's frame. 
	 */
	public JFrame getFrame(JFrame f)
	{
		return frame;
	}
	
	
	/**
	 * Returning GUI's index. 
	 */
	public int getIndex() {
		return index;
		
	}

	/**
	 *  Disposing current frame and GUI and returning visibility of previous GUI's frame.
	 */
	public void goBack() {
		AbstractGUI gui = null;
		if(index!=1)
		{
			gui=BoundryControl.GUIArr.get(index-1);
			gui.show();
		}
			BoundryControl.GUIArr.remove(index);
			frame.dispose();
		
		
	}

	/**
	 * Enable visibility of child's frame.  
	 */
	public void show() {
	
		frame.setVisible(true);
	}

}
